#include <stdio.h>

int min(int a, int b) {
    return a > b ? b : a;
}

int main(void) {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        char buffer[1001];
        scanf("%s", buffer);
        char* buffer_pos = buffer;
        int x_count = 0;
        int t_count = 0;
        int u_count = 0;
        while (*buffer_pos != '\0') {
            if (*buffer_pos == 'X') {
                x_count++;
            }
            if (*buffer_pos == 'T') {
                t_count++;
            }
            if (*buffer_pos == 'U') {
                u_count++;
            }
            buffer_pos++;
        }
        printf("%d\n", min(x_count, min(t_count, u_count)));
    }
    return 0;
}
